package com.stox.client.ui.javafx.widget.auto;

import javafx.scene.Node;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;

import com.stox.client.ui.javafx.widget.NumberSpinner;
import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.shared.core.beans.PropertyDescriptor;

public class PropertyAutoUI extends HBox {

	private final PropertyDescriptor propertyDescriptor;
	private final Node widget;
	
	public PropertyAutoUI(PropertyDescriptor propertyDescriptor) {
		this.propertyDescriptor = propertyDescriptor;
		UI.style(this, CSS.propertyAutoUI);
		
		if(Integer.class.isAssignableFrom(propertyDescriptor.getType())
				|| int.class.isAssignableFrom(propertyDescriptor.getType())){
			NumberSpinner numberSpinner = new NumberSpinner();
			numberSpinner.setStepWidth(1);
			numberSpinner.setValue(Integer.parseInt(propertyDescriptor.getValue().toString()));
			widget = numberSpinner;
		}else if(Double.class.isAssignableFrom(propertyDescriptor.getType())
				|| double.class.isAssignableFrom(propertyDescriptor.getType())){
			NumberSpinner numberSpinner = new NumberSpinner();
			numberSpinner.setStepWidth(0.01);
			numberSpinner.setValue(Double.parseDouble(propertyDescriptor.getValue().toString()));
			widget = numberSpinner;
		}else if(propertyDescriptor.getType().isEnum()){
			ComboBox<Object> comboBox = new ComboBox<Object>();
			comboBox.getItems().addAll(propertyDescriptor.getType().getEnumConstants());
			comboBox.getSelectionModel().select(propertyDescriptor.getValue());
			widget = comboBox;
		}else if(Boolean.class.equals(propertyDescriptor.getType()) ||
				boolean.class.equals(propertyDescriptor.getType())){
			CheckBox checkBox = new CheckBox();
			checkBox.setSelected(Boolean.parseBoolean(propertyDescriptor.getValue().toString()));
			widget = checkBox;
		}else
			throw new UnsupportedOperationException();
		
		
		Pane labelPane = new Pane();
		UI.style(labelPane, CSS.propertyAutoUILabelPane);
		Label label = new Label(propertyDescriptor.getDisplayName());
		setHgrow(label, Priority.ALWAYS);
		labelPane.getChildren().add(label);
		setHgrow(labelPane, Priority.ALWAYS);
		
		Pane widgetPane = new Pane();
		UI.style(widgetPane, CSS.propertyAutoUIWidgetPane);
		widgetPane.getChildren().add(widget);
		setHgrow(widgetPane, Priority.ALWAYS);
		UI.style(widget, CSS.propertyAutoUIWidget);
		getChildren().addAll(labelPane, widgetPane);
		
	}
	
	public void setValue(){
		if(Integer.class.isAssignableFrom(propertyDescriptor.getType())
				|| int.class.isAssignableFrom(propertyDescriptor.getType())){
			NumberSpinner numberSpinner = (NumberSpinner)widget;
			propertyDescriptor.setValue(numberSpinner.getValue().intValue());
		}else if(Double.class.isAssignableFrom(propertyDescriptor.getType())
				|| double.class.isAssignableFrom(propertyDescriptor.getType())){
			NumberSpinner numberSpinner = (NumberSpinner)widget;
			propertyDescriptor.setValue(numberSpinner.getValue().doubleValue());
		}else if(propertyDescriptor.getType().isEnum()){
			ComboBox<?> comboBox = (ComboBox<?>)widget;
			propertyDescriptor.setValue(comboBox.getValue());
		}else if(Boolean.class.equals(propertyDescriptor.getType()) ||
				boolean.class.equals(propertyDescriptor.getType())){
			CheckBox checkBox = (CheckBox)widget;
			propertyDescriptor.setValue(checkBox.isSelected());
		}
	}
	
}
